package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.entity.vo.AttrGroupVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author Syh
 * @email 1584442935@qq.com
 * @date 2022-02-07 14:29:04
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


//    /product/attrgroup/{catelogId}/withattr
    //获取分类下所有分组和该分组下的关联属性
    @GetMapping("/{catelogId}/withattr")
    public R selectRelationAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo> list = attrGroupService.selectRelationAndAttr(catelogId);
        return R.ok().put("data",list);
    }
//    attr/relation
//·     添加属性与分组关联关系
    @PostMapping("attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        attrGroupService.saveRelation(attrGroupRelationVo);
        return R.ok();
    }


//    /product/attrgroup/{attrgroupId}/noattr/relation
//获取属性分组没有关联的其他属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R selectNoRelationAttr(@RequestParam Map<String, Object> params,@PathVariable Long attrgroupId){
        PageUtils page = attrGroupService.selectRelationNoAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteReletion(@RequestBody List<AttrGroupRelationVo> list){
        attrGroupService.deleteReletionById(list);
        return R.ok();
    }



    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R selectReletionattr(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrGroupService.selectReletionattr(attrgroupId);
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据前端穿回来的三级分类菜单查询对应的数据
     * @param params
     * @return
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R GetCategoryById(@RequestParam Map<String, Object> params,@PathVariable("catelogId") String categoryId){
//        PageUtils page = attrGroupService.queryPage(params);

        IPage<AttrGroupEntity> list = attrGroupService.selectPageInfo(params,categoryId);
        return R.ok().put("page", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        //根据catelogid查询出父级id
        List<Long> list = attrGroupService.selectCatelogPath(attrGroup.getCatelogId());

        //放到返回的对象当中
        attrGroup.setCatelogPath(list);

        log.info("完整路径{}",list);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
